package com.exam.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.exam.dao.UserDao;
import com.exam.entity.User;


/**
 * Servlet implementation class ExcelServlet
 */
@WebServlet("/excel")
public class ExcelServlet extends HttpServlet {
	
	/**
	 * servlet设置返回值内容
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String filename="users.xls";
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		response.setHeader("Content-Disposition", "attachment; filename="+filename);
		response.setContentType("application/vnd.ms-excel;charset=UTF-8");//返回excel格式
		
		UserDao us=new UserDao();
		try {
			List<User> list=us.sel();
			out.println("role_id\t class_id\t user_name\t user_password\t user_phone\t user_sex");
			for (User user : list) {
				out.println(user.getRole_id()+"\t"+user.getClass_id()+"\t"+user.getUser_name()+"\t"+user.getUser_password()+"\t"+user.getUser_phone()+"\t"+user.getUser_sex());
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		out.flush();
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
